package com.ciwong.epaper.common.dialogbottom;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.ciwong.epaper.R;

public abstract class BaseBottomDialog extends Dialog implements View.OnClickListener {

    private final Window window;
    private WindowManager.LayoutParams lp;

    @Override
    public void setContentView(@NonNull View view) {
        super.setContentView(view);
        setUnifyWindow(window);
    }

    @Override
    public void setContentView(@NonNull int res) {
        super.setContentView(res);
        setUnifyWindow(window);
    }

    private void setUnifyWindow(Window window) {
        lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;
        window.setAttributes(lp);
    }

    public BaseBottomDialog(Context context) {
        this(context, -1);
    }

    public BaseBottomDialog(Context context, int style) {
        super(context, style);
        window = getWindow();
        initView(context);
    }

    public abstract void initView(Context context);

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.tl_close) {
            dismiss();
        }
    }

    int dpToPixel(float dp, Context context) {
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        return (int) (metrics.density * dp);
    }

}
